package com.meihua.baoleyi.net;

import android.content.Context;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.meihua.baoleyi.utils.UrlContants;
import com.meihua.baoleyi.view.ResetPwdView;

import org.json.JSONException;
import org.json.JSONObject;
import okhttp3.Call;
import okhttp3.Response;

/**
 * Name: liming
 * Time: 2017/10/16.
 */

public class PwdReset implements ResetClass<ResetPwdView> {

    private  ResetPwdView resetPwdView;
    private Context mContext;


    public PwdReset(ResetPwdView resetPwdView, Context mContext) {
        this.resetPwdView = resetPwdView;
        this.mContext = mContext;
    }

    @Override
    public void reset(final String newspass1, final String newspass2) {
        OkGo.post(UrlContants.RESET_PWD)
                .tag(this)
                .params("newspass1",newspass1)
                .params("newspass2",newspass2)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        if(s != null){
//                            Gson gson = new Gson();
//                            RegisterCodeBean registerCodeBean = gson.fromJson(s,RegisterCodeBean.class);
//                            System.out.println("原始信息重置密码参数：" + newspass1+"=="+newspass2);
//                            System.out.println("原始信息重置密码请求地址：" + UrlContants.RESET_PWD);
//                            System.out.println("原始信息重置密码：" + registerCodeBean.toString());
                            try {
                                JSONObject jsonObject = new JSONObject(s);
                                if (jsonObject.optString("code").equals("1")) {
                                    resetPwdView.onReset(newspass1,newspass2,jsonObject.optString("msg"),true);
                                }else {
                                    resetPwdView.onReset(null,null,jsonObject.optString("msg"),false);
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                        resetPwdView.onReset(null,null,e.toString(),false);
                    }
                });
    }

}
